/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-09-05 10:03:17
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-10-24 08:29:06
 * @FilePath: /react18-vite-admin/src/store/modules/permission.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { create } from 'zustand';
import { createBrowserRouter, Outlet } from 'react-router-dom';

import { PermissionStore, type RemixRouter } from '@/typings/store/permission';
import { createReactRouterObjects, constantRoutes } from '@/router';
import BeforeEach from '@/router/BeforeEach';
import { treeToArray, type TreeDataWithParent } from '@/utils';
import { type MenuTreeWithMetaItem } from '@/typings/store/permission';

export const usePermissionStore = create<PermissionStore>((set, get) => {
    return {
        needUpdate: true,
        setNeedUpdate: (needUpdate: boolean) => set({ needUpdate }),
        routes: [],
        setRoutes: (routes: AppRouteRecordRaw[]) => set({ routes }),
        addRoutes: [],
        setAddRoutes: (addRoutes: AppRouteRecordRaw[]) => set({ addRoutes }),
        reactRouter: createBrowserRouter(
            [
                {
                    path: '/',
                    element: (
                        <BeforeEach>
                            <Outlet />
                        </BeforeEach>
                    ),
                    children: createReactRouterObjects(constantRoutes)
                }
            ],
            {
                basename:
                    import.meta.env.MODE === 'development' ? '/admin' : './'
            }
        ),
        setReactRouter: (reactRouter?: RemixRouter) => set({ reactRouter }),
        redirectPaths: {},
        setRedirectPaths: (redirectPaths) => set({ redirectPaths }),
        menuTree: [],
        mixLeftMenuTree: [],
        setMenuTree: (menuTree) => set({ menuTree }),
        setMixLeftMenuTree: (mixLeftMenuTree) => set({ mixLeftMenuTree }),
        findNodeById(id) {
            const menuTree = JSON.parse(JSON.stringify(get().menuTree));
            const flatArr = treeToArray<MenuTreeWithMetaItem>(menuTree);
            for (let i = 0; i < flatArr.length; i++) {
                const node = flatArr[i];
                if (`${node.id}` === id) {
                    return node;
                }
            }
        },
        findNodeByPath(path: string) {
            const menuTree = JSON.parse(JSON.stringify(get().menuTree));
            const flatArr = treeToArray<MenuTreeWithMetaItem>(menuTree);
            for (let i = 0; i < flatArr.length; i++) {
                const node = flatArr[i];
                if (node.path === path) {
                    return node;
                }
            }
        }
    };
});

export const findRelatedNodes = (
    findPath: string,
    menuTree: MenuTreeWithMetaItem[]
) => {
    const flatArr = treeToArray<MenuTreeWithMetaItem>(menuTree);
    const node = flatArr.find((item) => item.path === findPath);
    const nodes: TreeDataWithParent<MenuTreeWithMetaItem>[] = [];
    const findParent = (
        node: TreeDataWithParent<MenuTreeWithMetaItem>
    ): MenuTreeWithMetaItem => {
        nodes.unshift(node);
        if (node.parent) {
            return findParent(node.parent);
        }
        return node;
    };
    if (node) {
        findParent(node);
    }
    return nodes;
};
